package org.tio.sitexxx.web.server.controller.minip;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Consultant;
import org.tio.sitexxx.service.model.main.Follower;
import org.tio.sitexxx.service.model.main.Member;

import org.tio.sitexxx.service.service.qj.ConsultantService;
import org.tio.sitexxx.service.service.qj.FollowerService;
import org.tio.sitexxx.service.service.qj.MemberService;
import org.tio.sitexxx.web.server.utils.ObjectToMap;

import java.util.*;

@RequestPath("/consultant")
public class ConsultantController {
    /**
     * 暂时没有用到
     * @return
     * @throws Exception
     */
    @RequestPath("/list")
    public Object list() throws Exception {
        JSONArray result = new JSONArray();
        Map<String, Object> query = new HashMap<>();
        List<Consultant> consultants = ConsultantService.me.getAll();
        for (int i = 0; i < consultants.size(); i++) {
            Map<String, Object> Consultant = new HashMap<>();

            Consultant=JSON.parseObject( JSON.toJSONString(consultants.get(i)));
            query.put("consultantId", consultants.get(i).getId());
            Consultant.put("number", ConsultantService.me.getDesignCount(query,consultants.get(i).getId()));
            result.add(Consultant);
        }
        System.out.println(result.toString());
        return result;
    }

    /**
     * 返回咨询的顾问列表
     * @return
     */
    @RequestPath("/instant/get")
    public Object instant_list()  {
        System.out.println("consultant list called");
        JSONArray result = new JSONArray();
        try{
            Map<String, Object> query = new HashMap<>();

            List<Consultant> consultants = ConsultantService.me.getAll();
            for (int i = 0; i < consultants.size(); i++) {
                Map<String, Object> Consultant=JSON.parseObject( JSON.toJSONString(consultants.get(i)));
                query.put("consultantId", consultants.get(i).getId());

                Consultant.put("number", ConsultantService.me.getDesignCount(query,consultants.get(i).getId()));
                result.add(Consultant);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return result;
    }

    /**
     * 根据id返回顾问信息
     * @param id
     * @return
     */
    @RequestPath("/instant/get/{id}")
    public Consultant Consultant(Integer id) {
        System.out.println("consultant id called");
        Map<String, Object> query = new HashMap<>();
        query.put("id", id);
        return ConsultantService.me.getConsultant(query);
    }

    /**
     * 暂时没用
     * @param id
     * @return
     */
    @RequestPath("/get/{id}")
    public Consultant getConsultantById(Integer id) {
        Map<String, Object> query = new HashMap<>();
        query.put("id", id);
        return ConsultantService.me.getConsultant(query);
    }

    /**
     * 关注顾问
     * @param request
     * @return
     */
    @RequestPath(value = "/follow")
    public Map<String, Object> follow(HttpRequest request) {
        Map<String,Object>body= JSON.parseObject(request.getBodyString());
        String openid = body.get("openid").toString();
        Integer consultant_id = Integer.valueOf(body.get("consultant_id").toString());

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> query = new HashMap<>();
        query.put("openid", openid);
        Member member = MemberService.me.getMember(query);
        Follower f1 = new Follower();
        f1.setConsultantId(consultant_id);
        f1.setMemberId(member.getId());
        Date date=new Date();
        f1.setCreatedAt(date);
        f1.setUpdatedAt(date);
        FollowerService.me.create(f1);
        resultMap.put("ok", 0);
        return resultMap;
    }

    /**
     * 取消关注顾问
     * @param request
     * @return
     */
    @RequestPath(value = "/unfollow")
    public Map<String, Object> unfollow(HttpRequest request) {
        Map<String,Object>body= JSON.parseObject(request.getBodyString());
        String openid = body.get("openid").toString();
        Integer consultant_id = Integer.valueOf(body.get("consultant_id").toString());
        Map<String, Object> query = new HashMap<>();
        query.put("openid", openid);
        Member member = MemberService.me.getMember(query);
        query.put("memberId", member.getId());
        query.put("consultantId", consultant_id);
        FollowerService.me.delete(query);
        query.clear();
        query.put("ok", 0);
        return query;
    }

    /**
     * 是否关注顾问
     * @param openid
     * @param consultant_id
     * @return
     */
    @RequestPath("/isfollow")
    public Map<String, Object> isFollow(String openid, Integer consultant_id) {
        Map<String, Object> query = new HashMap<>();
        query.put("openid", openid);
        Member member = MemberService.me.getMember(query);
        query.put("memberId", member.getId());
        System.out.println(consultant_id);
        query.put("consultantId", consultant_id);
        Follower follower = FollowerService.me.getFollower(query);
        query.clear();
        if (follower == null) {
            query.put("ok", 3);//未被关注
            return query;
        }
        query.put("ok", 0);//被关注
        return query;
    }

    /**
     * 查看关注顾问
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestPath("/myfollow")
    public Object myfollow(String openid) throws Exception {
        System.out.println("myfollow");
        Map<String, Object> resultMap = new HashMap<>(2);
        Map<String, Object> query = new HashMap<>(8);
        query.put("openid", openid);
        Member member = MemberService.me.getMember(query);
        query.put("memberId", member.getId());
        //关注表
        List<Follower> followers = FollowerService.me.getFollowers(query);
        if (followers.size() == 0) {
            resultMap.put("ok", 3);//没有关注顾问
            return resultMap;
        }
        List<Map<String, Object>> consultants = new ArrayList<>(8);
        for (int i = 0; i < followers.size(); i++) {
            query.put("id", followers.get(i).getConsultantId());
            Map<String, Object> Consultant=JSON.parseObject(JSON.toJSONString(ConsultantService.me.getConsultant(query)));
            if(null!=Consultant){
                query.put("consultantId", followers.get(i).getConsultantId());
                Integer number=ConsultantService.me.getDesignCount(query,followers.get(i).getConsultantId());
                System.out.println("number="+number);
                Consultant.put("number",number );
                consultants.add(Consultant);
            }
        }
        return consultants;
    }
}

